﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 进制转换
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine(comboBox1.SelectedIndex);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0 || comboBox2.SelectedIndex < 0)
            {
                MessageBox.Show("请选择 需要转换的进制......");
                return;
            }

            string str = tbInput.Text.Trim();
            if (str.Length < 1)
            {
                MessageBox.Show("请输入数字!");
                Console.WriteLine("======");
                return;
            }


            // 先将输入 转换成 10进制表示

            // 源 进制
            int srcIdx = comboBox1.SelectedIndex;
            int jz = -1;
            if (srcIdx == 0)
            {
                jz = 2;
            }
            else if (srcIdx == 1)
            {
                jz = 10;
            }
            else
            {
                jz = 16;
            }

            //
            long value = 0;
            try
            {
                value = Convert.ToInt64(str, jz);
                Console.WriteLine($"输入value : {value}");
            }
            catch
            {
                MessageBox.Show("输入的数值不合理!");
                return;
            }


            // 目标进制
            int dstJz = -1;
            int dstIdx = comboBox2.SelectedIndex;
            if (dstIdx == 0)
            {
                dstJz = 2;
            }
            else if (dstIdx == 1)
            {
                dstJz = 10;
            }
            else
            {
                dstJz = 16;
            }

            lbResult.Text = Convert.ToString(value, dstJz);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒");
        }

        private void btnChangeSysTime_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要修改系统时间么?", "信息提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                Console.WriteLine("OK");
                DateTime Year = this.dateTimePicker1.Value;

                SystemTime MySystemTime = new SystemTime();

                SetSystemDateTime.GetLocalTime(MySystemTime);

                Console.WriteLine($"{MySystemTime.vYear}-{MySystemTime.vMonth}-{MySystemTime.vDay}");

                MySystemTime.vYear = (ushort)this.dateTimePicker1.Value.Year;
                MySystemTime.vMonth = (ushort)this.dateTimePicker1.Value.Month;
                MySystemTime.vDay = (ushort)this.dateTimePicker1.Value.Day;
                MySystemTime.vHour = (ushort)this.dateTimePicker2.Value.Hour;
                MySystemTime.vMinute = (ushort)this.dateTimePicker2.Value.Minute;
                MySystemTime.vSecond = (ushort)this.dateTimePicker2.Value.Second;

                Console.WriteLine("=======最新的=================");
                Console.WriteLine($"{MySystemTime.vYear}-{MySystemTime.vMonth}-{MySystemTime.vDay}");

                try {
                    SetSystemDateTime.SetLocalTime(ref MySystemTime);
                }
                catch
                {
                    Console.WriteLine("===出现错误!");
                }
            }
        }
    }



}
